package ast;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import fuck.bean.Field;

/**
 * 类字段访问器,返回字段列表,可用于根据字段来构造自定义方法
 *
 */
public class FieldsVisitor extends ASTVisitor {

	List<Field> fieldList = new ArrayList<Field>();
	
	public List<Field> getFieldList() {
		return fieldList;
	}
	
	@Override
	public boolean visit(FieldDeclaration node) {
		
		//可能是int x, y . 所以这里会是循环
		for (Object obj : node.fragments()) {
			VariableDeclarationFragment vdf = (VariableDeclarationFragment) obj;
			
			Field f = new Field();
			f.setJavaClassName(node.getType().toString());
			f.setName(vdf.getName().toString());
			
			//忽略private public
			
			//javadoc
			Javadoc javadoc = node.getJavadoc();
			StringBuilder doc = new StringBuilder();
			if(null != javadoc){
				doc.append("");
				List<TagElement> tags = javadoc.tags();//注释和return分为两个TagElement,参数也为一个新的TagElement
				for(TagElement tag : tags){
					//注释的TagElement name为null
					//return的TagElement name 为@return
					doc.append(tag.getTagName() != null ? tag.getTagName() : "");
					
					List<TextElement> fragments = tag.fragments();
					for(TextElement te : fragments){
						//注释每一行为一个TextElement, TextElement的值Text为具体注释值
						doc.append(te.getText()).append("\n");
					}
					
				}
				
			}
			f.setExplain(doc.toString());
			
			fieldList.add(f);
		}
		
		//返回false表示不遍历器子节点
		return true;
	};
	
}
